package source

import java.util.Properties

import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.api.scala._
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.client.{Connection, ConnectionFactory, Put, Table}
import org.apache.hadoop.hbase.{HBaseConfiguration, TableName}

/**
  * @author dinghao 
  * @create 2021-09-09 10:45 
  * @message
  */
object FHBaseSource {
  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1)
    val properties = new Properties()
    properties.setProperty("bootstrap.servers","192.168.1.190:9092,192.168.1.192:9092,192.168.1.193:9092")
    properties.setProperty("group.id","consumer1")
    properties.setProperty("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer")
    properties.setProperty("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer")
    properties.setProperty("auto.offset.reset", "latest")
    val kafkaData:DataStream[String] = env.addSource(new FlinkKafkaConsumer011[String](
      "xiaofei",new SimpleStringSchema(),properties))
    kafkaData.map(writeHBase(_))
    env.execute("kafka-hbase")
  }

  def writeHBase(x: String) = {
    val conf: Configuration = HBaseConfiguration.create()
    conf.set("hbase.zookeeper.quorum","192.168.1.190:2181,192.168.1.192:2181,192.168.1.193:2181")
    val conn: Connection = ConnectionFactory.createConnection(conf)
    val table: Table = conn.getTable(TableName.valueOf("stu"))
    val arr: Array[String] = x.split(" ")
    if (arr.length != 3){
      println(x+"----插入失败，格式不匹配")
      "true"
    }else{
      val put = new Put("rk001".getBytes)
      put.addColumn("info".getBytes,arr(1).getBytes,arr(2).getBytes)
      table.put(put)
      println(x+"--- 添加成功")
      "false"
    }
  }
}
